class util {
  formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
  }

  /**
   * 转换时间
   */
    myTime(date){
      var arr = date.split("T");
      var d = arr[0];
      var darr = d.split('-');
      var t = arr[1];
      var tarr = t.split('.000');
      var marr = tarr[0].split(':');
      var dd = parseInt(darr[0]) + "-" + parseInt(darr[1]) + "-" + parseInt(darr[2]) + " " + parseInt(marr[0]) + ":" + parseInt(marr[1]) + ":" + parseInt(marr[2]);
      return dd;
    }



  formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(x => {
      x = x.toString()
      return x[1] ? x : '0' + x
    }).join('-') + ' ' + [hour, minute, second].map(x => {
      x = x.toString()
      return x[1] ? x : '0' + x
    }).join(':')
  }
     //计算天数
     checkDate(start_date, end_date) {
      // //日期格式化
      // var start_date = new Date(startTime.replace(/-/g, "/"));
      // var end_date = new Date(endTime.replace(/-/g, "/"));
      //转成毫秒数，两个日期相减
      // var ms = end_date.getTime() - start_date.getTime();
      var ms=end_date-start_date;
      console.log(ms,'checkDate-msms');
      //转换成天数
      var day = parseInt(ms / (1000 * 60 * 60 * 24));
      return day;
    }
  formatDate = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()

    return [year, month, day].map(x => {
      x = x.toString()
      return x[1] ? x : '0' + x
    }).join('-')
  }
  todate(date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    // minute = minute < 10 ? ('0' + minute) : minute;
    // second = second < 10 ? ('0' + second) : second;
    return y + '年' + m + '月' + d + '日';
  }
  toDate(number) {
    var n = number;
    console.log(number, "number");
    var date = new Date(parseInt(n) * 1000);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    // minute = minute < 10 ? ('0' + minute) : minute;
    // second = second < 10 ? ('0' + second) : second;
    return y + '年' + m + '月' + d + '日';
  }

  //当前日期加减天数
  mathChangeDate(date, method, days) {
    //method:'+' || '-'
    //ios不解析带'-'的日期格式，要转成'/'，不然Nan，切记
    // var dateVal = date.replace(/-/g, '/');
    var timestamp = Date.parse(date);
    if (method == '+') {
      timestamp = timestamp / 1000 + 24 * 60 * 60 * days;
    } else if (method == '-') {
      timestamp = timestamp / 1000 - 24 * 60 * 60 * days;
    }
    return timestamp;
  }
  //月日
  formatMD(date, addDayCount) {
    var dd=date;
    dd.setDate(dd.getDate() + addDayCount)
    var month = dd.getMonth() + 1
    var day = dd.getDate()
    return  month + "-" + day ;
  }

  decodeurl(str) {
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/&amp;/g, "&");
    s = s.replace(/&lt;/g, "<");
    s = s.replace(/&gt;/g, ">");
    s = s.replace(/&nbsp;/g, " ");
    s = s.replace(/&#39;/g, "\'");
    s = s.replace(/&quot;/g, "\"");
    return s;
  }


  setwatcher(data, watch) {
    Object.keys(watch).forEach(x => {
      this.observer(data, x, watch[x])
    })
  }

  observer(obj, key, watchFun) {
    var val = obj[key]; // 给该属性设默认值
    Object.defineProperty(obj, key, {
      configurable: true,
      enumerable: true,
      set: function(value) {
        val = value;
        watchFun(value, val); // 赋值(set)时，调用对应函数
      },
      get: function() {
        return val;
      }
    })
  }

}

module.exports = util